package com.carbohydrate42.kotlin_compose_demo.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.carbohydrate42.kotlin_compose_demo.api.AuthApi
import com.carbohydrate42.kotlin_compose_demo.dto.vo.UserVo
import com.carbohydrate42.kotlin_compose_demo.module.MyRefreshFooter
import com.carbohydrate42.kotlin_compose_demo.module.MyRefreshHeader
import com.carbohydrate42.kotlin_compose_demo.module.SmartSwipeRefresh
import com.carbohydrate42.kotlin_compose_demo.module.SmartSwipeStateFlag
import com.carbohydrate42.kotlin_compose_demo.module.drawVerticalScrollbar
import com.carbohydrate42.kotlin_compose_demo.module.rememberSmartSwipeRefreshState


@Preview(showBackground = true, showSystemUi = true)
@Composable
fun InfiniteListDemo() {
    val refreshState = rememberSmartSwipeRefreshState()
    val lazyListState = rememberLazyListState()

    val userVoList = remember {
        emptyList<UserVo>().toMutableStateList()
    }

    LaunchedEffect(Unit) {
        val all = AuthApi.instance.all().data ?: emptyList()
        userVoList.clear()
        userVoList.addAll(all)
        refreshState.refreshFlag = SmartSwipeStateFlag.SUCCESS
    }

    Column {
        SmartSwipeRefresh(
            onRefresh = {
                try {
                    val all = AuthApi.instance.all().data ?: emptyList()
                    userVoList.clear()
                    userVoList.addAll(all)
                    refreshState.refreshFlag = SmartSwipeStateFlag.SUCCESS
                } catch (e: Throwable) {
                    refreshState.refreshFlag = SmartSwipeStateFlag.ERROR
                }
            },
            onLoadMore = {
                try {
                    val all = AuthApi.instance.all().data ?: emptyList()
                    userVoList.addAll(all)
                    refreshState.loadMoreFlag = SmartSwipeStateFlag.SUCCESS
                } catch (e: Throwable) {
                    refreshState.loadMoreFlag = SmartSwipeStateFlag.ERROR
                }
            },
            state = refreshState,
            isNeedRefresh = true,
            isNeedLoadMore = true,
            headerIndicator = {
                MyRefreshHeader(refreshState.refreshFlag, true)
            },
            footerIndicator = {
                MyRefreshFooter(refreshState.loadMoreFlag, true)
            }
        ) {
            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .fillMaxHeight()
                    .drawVerticalScrollbar(lazyListState),
                state = lazyListState
            ) {
                itemsIndexed(userVoList) { index, user ->
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .background(if (index % 2 == 0) Color.LightGray else Color.White)
                            .padding(16.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Row(
                            horizontalArrangement = Arrangement.SpaceBetween,
                            modifier = Modifier
                                .fillMaxWidth()
                                .fillMaxHeight()
                        ) {
                            Row {
                                Box(
                                    modifier = Modifier
                                        .width(32.dp)
                                        .height(32.dp)
                                        .background(color = Color.Yellow)
                                )
                                Spacer(modifier = Modifier.width(16.dp))
                                Text(text = user.name.toString())
                            }

                        }
                    }
                }
            }
        }
    }


}
